<template>
  <div>

    <template>
      <el-form :inline="true" :model="formInline" class="demo-form-inline">

        <el-form-item label="字典名称">
          <el-input v-model="formInline.dictName" placeholder="请输入字典名称"></el-input>
        </el-form-item>
        <el-form-item label="字典类型">

          <el-input v-model="formInline.sysDictType" placeholder="请输入字典类型"></el-input>

        </el-form-item>
        <el-form-item label="状态">
          <el-select v-model="formInline.status" placeholder="字典状态">
            <el-option label="正常" value="0"></el-option>
            <el-option label="停用" value="1"></el-option>
          </el-select>
        </el-form-item><br>
        <el-form-item label="创建时间">
          <el-col :span="11">
            <el-date-picker type="date" placeholder="开始日期" v-model="form.startTime" style="width: 100%;"></el-date-picker>
          </el-col>

          <el-col :span="11">
            <el-date-picker type="date" placeholder="结束日期" v-model="form.endTime"   style="width: 100%;"></el-date-picker>
          </el-col>
        </el-form-item>


        <el-form-item>
          <el-button type="primary" @click="list()">查询</el-button>
          <el-button type="primary"  @click="reset()">重置</el-button>


        </el-form-item>
      </el-form>
<!--      <el-button @click="updateopen(scope.row)" type="text" size="small">详情</el-button>-->

<!--      <el-button @click="deletebyid(scope.row.postId)" type="text" size="small">删除</el-button>-->
      <el-button  @click="openadd()" type="primary" >新增</el-button>
      <el-button type="success" disabled>修改</el-button>
      <el-button type="warning" disabled>删除</el-button>
      <el-button @click="excel()"  type="danger" >导出</el-button>
      <el-table
          ref="multipleTable"
          :data="tableData"
          tooltip-effect="dark"
          style="width: 100%"
          @selection-change="handleSelectionChange">
        <el-table-column
            type="selection"
            width="55">
        </el-table-column>

        <el-table-column
            prop="dictId"
            label="字典编号"
            width="120">
        </el-table-column>

        <el-table-column
            prop="dictName"
            label="字典名字"
            width="120">
        </el-table-column>

        <el-table-column
          prop="dictType"
          label="字典类型"
          width="120">
          <template slot-scope="a">
            <el-button  @click="zdlist(a.row.dictType)" type="text" size="small">
              <span>{{a.row.dictType}}</span>
            </el-button>


          </template>

        </el-table-column>

        <el-table-column
            prop="createTime"
            label="创建时间"
            width="120">
        </el-table-column>

        <el-table-column
            prop="status"
            label="状态"
            width="120">
          <template slot-scope="a">
            <el-button v-if="a.row.status==0" type="text" size="small">正常</el-button>
            <el-button v-if="a.row.status!=0" type="text" size="small">停用</el-button>

          </template>
        </el-table-column>

        <el-table-column
            prop="remark"
            label="备注"
            width="120">
        </el-table-column>
        <el-table-column
            fixed="right"
            label="操作"
            width="100">
          <template slot-scope="scope">
            <el-button @click="updateopen(scope.row)" type="text" size="small">修改</el-button>
            <el-button @click="deletebyid(scope.row)" type="text" size="small">删除</el-button>

          </template>
        </el-table-column>
      </el-table>

    </template>
    <el-dialog title="添加" :visible.sync="dialogFormVisible" >
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="字典名字" :label-width="formLabelWidth" prop="dictName">
          <el-input v-model="form.dictName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典类型" :label-width="formLabelWidth" prop="sysDictType">
          <el-input v-model="form.dictType" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="岗位状态" :label-width="formLabelWidth" prop="status">
          <el-radio v-model="form.status" label="0">正常</el-radio>
          <el-radio v-model="form.status" label="1">停用</el-radio>

        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="add('ruleForm')">添加</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改" :visible.sync="dialogFormVisibletwo">
      <el-form :model="form" :rules="rules" ref="ruleForm" label-width="100px" class="demo-ruleForm">
        <el-form-item label="字典名字" :label-width="formLabelWidth" prop="dictName">
          <el-input v-model="form.dictName" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="字典类型" :label-width="formLabelWidth" prop="sysDictType">
          <el-input v-model="form.dictType" autocomplete="off"></el-input>
        </el-form-item>

        <el-form-item label="岗位状态" :label-width="formLabelWidth" prop="status">
          <el-radio v-model="form.status" label="0">正常</el-radio>
          <el-radio v-model="form.status" label="1">停用</el-radio>

        </el-form-item>
        <el-form-item label="备注" :label-width="formLabelWidth" prop="remark">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="创建时间" :label-width="formLabelWidth" prop="remark">
          <el-input v-model="form.remark" autocomplete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="update('ruleForm')">修改</el-button>
      </div>
    </el-dialog>
    <el-pagination
        @current-change="handleCurrentChange"
        :page-size="pageSize"
        layout="total, prev, pager, next"
        :total="total">
    </el-pagination>
  </div>
</template>

<script>
export default {
  name: "DictDataView",
  data() {
    return {
      ids:[],
      pageNum: 1,
      total: 0,
      pageSize: 3,
      dialogFormVisibletwo: false,
      formInline: {},
      formLabelWidth: "120px",
      form: {},
      tableData: [],
      dialogFormVisible: false,
      multipleSelection: [],
      sysDictType:'',
      rules: {
        dictName: [
          {required: true, message: '请输入字典名称', trigger: 'blur'},
        ],
        sysDictType: [
          {required: true, message: '请输入字典类型', trigger: 'blur'},
        ],
        remark: [
          {required: true, message: '请输入备注', trigger: 'blur'},
        ],
        status: [
          {required: true, message: '请输入状态', trigger: 'blur'},
        ],
      }
    }
  }, methods: {
    zdlist(dictType){
      localStorage.setItem("dictType",dictType);
     this.$router.push("/DictType");
    },
    reset(){
      window.location.reload()
    },

  listAll(){
    this.axios.post("/system/dict/dictlistall?pageNum="+this.pageNum).then(res=>{
      this.tableData=res.data.list;
      this.total=res.data.total;
    })
  },
    handleCurrentChange(page) {
      this.pageNum = page;
      this.listAll(this.pageNum);
    },
    handleSelectionChange(val) {
        console.log(val)
      this.multipleSelection = val;
      for (let i = 0; i <val ; i++) {
        this.ids[i]=val[i].dictId;
      }
      console.log(this.ids)

    },
    excel(){
      location="http://localhost:9017/dict/excel";

    },
    deletebyid(row){
    this.form=row;
      this.axios.post("system/dict/delete",this.form).then(res=>{
        if (res.status==200){
          this.$message.success("删除成功");

          this.listAll();
        }else{
          this.$message.error("删除失败");
        }
      })
    },
    update(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post("system/dict/update",this.form).then(res=>{
            if (res.status==200){
              this.$message.success("修改成功");
              this.dialogFormVisibletwo=false;
              this.list();
            }else{
              this.$message.error("修改失败");
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });


    },
    updateopen(row){
      console.log(row.dictId)
      this.dialogFormVisibletwo=true;
      this.form=row;

    },
    add(formName){
      this.$refs[formName].validate((valid) => {
        if (valid) {
          this.axios.post("system/dict/add",this.form).then(res=>{

            if (res.status==200){
              this.$message.success("添加成功");
              this.dialogFormVisible=false;
              this.list();
            }else{
              this.$message.error("添加失败");
            }
          })
        } else {
          console.log('error submit!!');
          return false;
        }
      });


    },
    openadd(){
      this.dialogFormVisible=true;
      this.form={"status":"0"};

    },

    list(){
      this.axios.post("/system/dict/dictlist",this.formInline).then(res=>{
        this.tableData=res.data;
      })
    }
  }, created() {
   this.listAll();



  }
}
</script>

<style scoped>

</style>